"""
批量插入对话框
提供用户友好的批量插入行界面
"""

from PySide6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, 
                              QSpinBox, QComboBox, QPushButton, QDialogButtonBox,
                              QGroupBox, QRadioButton, QButtonGroup)
from PySide6.QtCore import Qt, Signal
from typing import Optional


class BatchInsertDialog(QDialog):
    """批量插入对话框"""
    
    def __init__(self, parent=None, current_row: int = 0, total_rows: int = 0):
        super().__init__(parent)
        self.current_row = current_row
        self.total_rows = total_rows
        self._setup_ui()
        
    def _setup_ui(self):
        """设置UI"""
        self.setWindowTitle("批量插入行")
        self.setModal(True)
        self.resize(400, 300)
        
        layout = QVBoxLayout(self)
        
        # 行数设置
        row_group = QGroupBox("插入行数")
        row_layout = QVBoxLayout(row_group)
        
        row_input_layout = QHBoxLayout()
        row_input_layout.addWidget(QLabel("插入行数:"))
        self.row_spinbox = QSpinBox()
        self.row_spinbox.setRange(1, 1000)
        self.row_spinbox.setValue(1)
        row_input_layout.addWidget(self.row_spinbox)
        row_layout.addLayout(row_input_layout)
        
        layout.addWidget(row_group)
        
        # 插入位置
        pos_group = QGroupBox("插入位置")
        pos_layout = QVBoxLayout(pos_group)
        
        self.pos_group = QButtonGroup()
        
        self.before_radio = QRadioButton(f"在当前行之前 (第 {self.current_row + 1} 行)")
        self.pos_group.addButton(self.before_radio)
        pos_layout.addWidget(self.before_radio)
        
        self.after_radio = QRadioButton(f"在当前行之后 (第 {self.current_row + 2} 行)")
        self.pos_group.addButton(self.after_radio)
        pos_layout.addWidget(self.after_radio)
        
        self.end_radio = QRadioButton("在末尾添加")
        self.pos_group.addButton(self.end_radio)
        pos_layout.addWidget(self.end_radio)
        
        # 默认选择
        if self.current_row < self.total_rows:
            self.after_radio.setChecked(True)
        else:
            self.end_radio.setChecked(True)
        
        layout.addWidget(pos_group)
        
        # 填充模式
        fill_group = QGroupBox("填充模式")
        fill_layout = QVBoxLayout(fill_group)
        
        self.fill_combo = QComboBox()
        self.fill_combo.addItems([
            "智能默认值",
            "空白值",
            "复制上一行",
            "复制当前行"
        ])
        fill_layout.addWidget(self.fill_combo)
        
        layout.addWidget(fill_group)
        
        # 按钮
        button_box = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel
        )
        button_box.accepted.connect(self.accept)
        button_box.rejected.connect(self.reject)
        layout.addWidget(button_box)
        
    def get_insert_config(self) -> dict:
        """获取插入配置"""
        if self.before_radio.isChecked():
            position = self.current_row
        elif self.after_radio.isChecked():
            position = self.current_row + 1
        else:
            position = self.total_rows
            
        fill_modes = ["smart", "empty", "copy_above", "copy_current"]
        fill_mode = fill_modes[self.fill_combo.currentIndex()]
        
        return {
            "count": self.row_spinbox.value(),
            "position": position,
            "fill_mode": fill_mode
        }